package com.yutel.system.struts.extjs;

import com.yutel.system.repository.ExtPanel;

public class ExtjsEngine {
	public void make(ExtPanel panel){
		panel.setRid(panel.getId());
		sb.append("Ext.onReady(function() { \n");
		sb.append(subPanel(panel));
		sb.append(" var p").append(panel.getId()).append("=new P").append(panel.getId());
		sb.append("({width:window.screen.availWidth-215,height:window.screen.availHeight-255});");
		sb.append(" p").append(panel.getId()).append(".render('main").append(panel.getId()).append("');");
		sb.append("});");
		sb.append("</script>");
		sb.append("<div id='main").append(panel.getId()).append("'></div>");
	}
	public String subPanel(ExtPanel panel) {
		StringBuffer var=new StringBuffer();
		StringBuffer ssb=new StringBuffer();
		ssb.append("P").append(panel.getId()).append("= Ext.extend(Ext.Panel, { \n");
		ssb.append("  initComponent : function() { \n");
		for(ExtPanel p : panel.getSet()) {
			p.setRid(panel.getRid());
			ssb.append("var p").append(p.getId()).append("=new P").append(p.getId()).append("();\n");
			var.append("p").append(p.getId()).append(",");
			if("tree".equals(p.getType())) {
				BasePanel tp=new TreePanel();
				ssb.insert(0, tp.make(p));
			} else if("grid".equals(p.getType())){
				BasePanel gp=new GridPanel();
				ssb.insert(0, gp.make(p));
			} else {
				ssb.insert(0, subPanel(p));
			}
		}
		ssb.append("Ext.apply(this, {");
		ssb.append("id:'wyid").append(panel.getId()).append("',");
		ssb.append("title:'").append(panel.getName()).append("',");
		ssb.append("frame : true,border:true,");
		if(panel.getWidth()!=null && panel.getWidth()>0) {
			ssb.append("width : ").append(panel.getWidth()).append(", \n");
		}
		if(panel.getHeight()!=null && panel.getHeight()>0) {
			ssb.append("height : ").append(panel.getHeight()).append(", \n");
		}
		if(panel.getLayout()!=null && !"".equals(panel.getLayout())) {
			ssb.append("layout : '").append(panel.getLayout()).append("', \n");
		}
		if(panel.getRegion()!=null && !"".equals(panel.getRegion())) {
			ssb.append("region : '").append(panel.getRegion()).append("', \n");
		}
		if(panel.getType()!=null && !"panel".equals(panel.getType())) {
			ssb.append("html:'测试内容', \n");
		}
		if(panel.getSet().size()>0) {
			var.deleteCharAt(var.length()-1);
			ssb.append(" items : [").append(var.toString()).append("] \n");
		}
		ssb.append("}); \n");
		ssb.append("P").append(panel.getId()).append(".superclass.initComponent.call(this);");
		ssb.append("} \n");
		ssb.append("}); \n");
		return ssb.toString();
	}
	public static void main(String[] args){
		ExtjsEngine ep=new ExtjsEngine();
		System.out.println(ep.getSb().toString());
	}
	
	public ExtjsEngine() {
		sb=new StringBuffer();
	}

	private StringBuffer sb;
	public StringBuffer getSb() {
		return sb;
	}
	public void setSb(StringBuffer sb) {
		this.sb = sb;
	}

}
